
$(function() {

    $(document).ready(function() {
        $('#ok-secretario').click(function() {
            var info = {
                nome: document.getElementsByName('nome')[1].value,
                username: document.getElementsByName('username')[1].value,
                password: document.getElementsByName('password')[1].value,
                email: document.getElementsByName('email')[1].value,
                matricula: document.getElementsByName('matricula')[1].value,
                telefone: document.getElementsByName('telefone')[0].value
            };
            $.post('/new/secretario',
                info,
                function(event) {
                    $('#add-sec-modal').modal().trigger('hide');
                    var tags = document.getElementsByTagName('form');
                    for (var i = 0; i < tags.length; i++) {
                        tags[i].reset();
                    }
                });
        });
        $('#ok-professor').click(function() {
            var selected = $('.ui-selected');
            var preferidas = [];
            selected.each(function(i) {
                preferidas.push(selected[i].textContent);
            });
            var info = {
                nome: document.getElementsByName('nome')[1].value,
                username: document.getElementsByName('username')[1].value,
                password: document.getElementsByName('password')[1].value,
                email: document.getElementsByName('email')[1].value,
                matricula: document.getElementsByName('matricula')[1].value,
                departamento: $('#departamento').val(),
                preferidas: preferidas
            };
            $.post('/new/professor', info, 'json');
        });
        $('#ok-departamento').click(function() {
            $.post('/new/departamento', {
                nome: $('#dep-nome').val()
            }, function(event) {
                $('#add-dep-modal').modal().trigger('hide');
                document.getElementById('form-departamento').reset();
            });
        });
        $('#ok-curso').click(function() {
            $.post('/new/curso', {
                nome: $('#cur-nome').val(),
                departamento: $('#cur-departamento').val()
            }, function(event) {
                $('#add-cur-modal').modal().trigger('hide');
                document.getElementById('form-curso').reset();
            });
        });
        $("#ok-disciplina").click(function() {
            $.post('/new/disciplina', {
                nome: $('#dis-nome').val(),
                carga: $('#dis-carga').val(),
                curso: $('#dis-curso').val()
            }, function(event) {
                $('#add-dis-modal').modal().trigger('hide');
                document.getElementById('form-disciplina').reset();
            });
        });
        $('#ok-turma').click(function() {
            var horarios = $('.horario');
            var values = [];
            for (var i=0; i < horarios.length; i++) {
                if (horarios[i].value !== "")
                    values.push(horarios[i].value);
            }
            /*
            for (var i=0; i<3; i++) {
                values.push(tags.slice(i, i+3);
            }*/
            $.post('/new/turma', {
                disciplina: $('#tur-disciplina').val(),
                vagas: $('#tur-vagas').val(),
                horario: values
            }, function(event){
                $('#add-tur-modal').modal().trigger('hide');
                document.getElementById('form-turma').reset();
            });
        });
    });
});
